package org.dromara.nursing.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.nursing.domain.bo.MsAdmissionContractBo;
import org.dromara.nursing.domain.vo.MsAdmissionContractVo;
import org.dromara.nursing.domain.MsAdmissionContract;
import org.dromara.nursing.mapper.MsAdmissionContractMapper;
import org.dromara.nursing.service.IMsAdmissionContractService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 入住合同111Service业务层处理
 *
 * @author Lion Li
 * @date 2025-03-16
 */
@RequiredArgsConstructor
@Service
public class MsAdmissionContractServiceImpl implements IMsAdmissionContractService {

    private final MsAdmissionContractMapper baseMapper;

    /**
     * 查询入住合同111
     *
     * @param contractId 主键
     * @return 入住合同111
     */
    @Override
    public MsAdmissionContractVo queryById(Long contractId){
        return baseMapper.selectVoById(contractId);
    }

    /**
     * 分页查询入住合同111列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 入住合同111分页列表
     */
    @Override
    public TableDataInfo<MsAdmissionContractVo> queryPageList(MsAdmissionContractBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MsAdmissionContract> lqw = buildQueryWrapper(bo);
        Page<MsAdmissionContractVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的入住合同111列表
     *
     * @param bo 查询条件
     * @return 入住合同111列表
     */
    @Override
    public List<MsAdmissionContractVo> queryList(MsAdmissionContractBo bo) {
        LambdaQueryWrapper<MsAdmissionContract> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MsAdmissionContract> buildQueryWrapper(MsAdmissionContractBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MsAdmissionContract> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(MsAdmissionContract::getContractId);
        lqw.eq(bo.getElderId() != null, MsAdmissionContract::getElderId, bo.getElderId());
        lqw.eq(StringUtils.isNotBlank(bo.getContractNo()), MsAdmissionContract::getContractNo, bo.getContractNo());
        lqw.eq(bo.getStartDate() != null, MsAdmissionContract::getStartDate, bo.getStartDate());
        lqw.eq(bo.getEndDate() != null, MsAdmissionContract::getEndDate, bo.getEndDate());
        lqw.eq(bo.getRoomId() != null, MsAdmissionContract::getRoomId, bo.getRoomId());
        lqw.eq(StringUtils.isNotBlank(bo.getBedNo()), MsAdmissionContract::getBedNo, bo.getBedNo());
        lqw.eq(StringUtils.isNotBlank(bo.getContractType()), MsAdmissionContract::getContractType, bo.getContractType());
        lqw.eq(bo.getMonthlyFee() != null, MsAdmissionContract::getMonthlyFee, bo.getMonthlyFee());
        lqw.eq(bo.getDeposit() != null, MsAdmissionContract::getDeposit, bo.getDeposit());
        lqw.eq(StringUtils.isNotBlank(bo.getPaymentMethod()), MsAdmissionContract::getPaymentMethod, bo.getPaymentMethod());
        lqw.eq(StringUtils.isNotBlank(bo.getPaymentCycle()), MsAdmissionContract::getPaymentCycle, bo.getPaymentCycle());
        lqw.eq(StringUtils.isNotBlank(bo.getSignedBy()), MsAdmissionContract::getSignedBy, bo.getSignedBy());
        lqw.eq(StringUtils.isNotBlank(bo.getSignerRelationship()), MsAdmissionContract::getSignerRelationship, bo.getSignerRelationship());
        lqw.eq(StringUtils.isNotBlank(bo.getContractStatus()), MsAdmissionContract::getContractStatus, bo.getContractStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getTerminationReason()), MsAdmissionContract::getTerminationReason, bo.getTerminationReason());
        lqw.eq(StringUtils.isNotBlank(bo.getSpecialTerms()), MsAdmissionContract::getSpecialTerms, bo.getSpecialTerms());
        lqw.eq(StringUtils.isNotBlank(bo.getFilePath()), MsAdmissionContract::getFilePath, bo.getFilePath());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), MsAdmissionContract::getRemarks, bo.getRemarks());
        return lqw;
    }

    /**
     * 新增入住合同111
     *
     * @param bo 入住合同111
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(MsAdmissionContractBo bo) {
        MsAdmissionContract add = MapstructUtils.convert(bo, MsAdmissionContract.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setContractId(add.getContractId());
        }
        return flag;
    }

    /**
     * 修改入住合同111
     *
     * @param bo 入住合同111
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(MsAdmissionContractBo bo) {
        MsAdmissionContract update = MapstructUtils.convert(bo, MsAdmissionContract.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MsAdmissionContract entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除入住合同111信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
